package com.example.villagemanager.ui.base;

import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import com.example.villagemanager.manager.DialogManager;
import com.example.villagemanager.model.event.Event;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import butterknife.ButterKnife;
import butterknife.Unbinder;

public abstract class  LazyloadFragment extends Fragment {
    private Unbinder unbinder;
    public Bundle mBundle;
    private ProgressDialog loadingDialog;
    private static final String NAME = "name";
    private String titlename ;

    private Unbinder unBinder;
    protected View rootView;
    private boolean isInitView = false;
    private boolean isVisible = false;
    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        if (getArguments() != null) {
            titlename = getArguments().getString(NAME);
        }

    }
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        rootView = inflater.inflate(setContentView(), container, false);
        unbinder = ButterKnife.bind(this, rootView);
        init();
        isInitView = true;
        isCanLoadData();
        return rootView;
    }

    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);

        //isVisibleToUser这个boolean值表示:该Fragment的UI 用户是否可见，获取该标志记录下来
        if(isVisibleToUser){
            isVisible = true;
            isCanLoadData();
        }else{
            isVisible = false;
        }
    }

    private void isCanLoadData(){
        //所以条件是view初始化完成并且对用户可见
        if(isInitView && isVisible ){
            lazyLoad();

            //防止重复加载数据
            isInitView = false;
            isVisible = false;
        }
    }

    /**
     * 加载页面布局文件
     * @return
     */
    protected abstract int setContentView();

    /**
     * 让布局中的view与fragment中的变量建立起映射
     */
    protected abstract void init();

    /**
     * 加载要显示的数据
     */
    protected abstract void lazyLoad();
    /**
     * 获取event
     *
     * @param event
     */
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMainThreadEvent(Event event) {
        if (null != event) {
            //处理事件...
            onEventProcess(event);
        }
    }

    /**
     * 处理event事件
     *
     * @param event
     */
    protected abstract void onEventProcess(Event event);

    /**
     * 跳转activity
     *
     * @param clazz
     */
    protected void sendIntent(Class<?> clazz) {
        sendIntent(clazz, null);
    }

    /**
     * 跳转activity并传值
     *
     * @param clazz
     * @param bundle
     */
    protected void sendIntent(Class<?> clazz, Bundle bundle) {
        Intent intent = new Intent(getActivity(), clazz);
        if (null != bundle) {
            intent.putExtras(bundle);
        }
        startActivity(intent);
    }

    /**
     * 跳转activity获取返回
     *
     * @param clazz
     * @param requestCode
     */
    protected void sendIntentForResult(Class<?> clazz, int requestCode) {
        sendIntentForResult(clazz, requestCode, null);
    }

    /**
     * 跳转activity获取返回并传值
     *
     * @param clazz
     * @param requestCode
     * @param bundle
     */
    protected void sendIntentForResult(Class<?> clazz, int requestCode, Bundle bundle) {
        Intent intent = getActivity().getIntent().setClass(getActivity(),clazz);
        if (null != bundle) {
            intent.putExtras(bundle);
        }
        startActivityForResult(intent, requestCode);
    }

    /**
     * 获取传值
     * @return
     */
    protected Bundle getBundle(){
        Intent intent = getActivity().getIntent();
        if (mBundle == null && intent != null) {
            mBundle = intent.getExtras();
        }
        return mBundle;
    }

    /**
     * 加载对话框
     * @return
     */
    protected ProgressDialog getLoadingDialog(){
        if(loadingDialog == null){
            loadingDialog = DialogManager.getLoadingDialog(getActivity());
        }
        return loadingDialog;
    }


    /**
     * 加载对话框
     * @param message
     * @return
     */
    protected ProgressDialog getLoadingDialog(String message){
        if(loadingDialog == null){
            loadingDialog = DialogManager.getLoadingDialog(getActivity());
        }
        loadingDialog.setMessage(message);
        return loadingDialog;
    }

    public abstract void InitLazy();
}
